package org.broadinstitute.hellbender;

import org.broadinstitute.hellbender.utils.bwa.BwaMemAligner;
import org.broadinstitute.hellbender.utils.bwa.BwaMemAlignment;
import org.broadinstitute.hellbender.utils.bwa.BwaMemIndex;
import org.testng.Assert;

import java.util.Collections;
import java.util.List;

/**
 * Created by shuang on 3/19/17.
 */
public class BwaMemTestUtils {

    public static void assertCorrectSingleReadAlignment(final BwaMemIndex index) {
        try ( final BwaMemAligner aligner = new BwaMemAligner(index) ) {
            //real read taken from src/test/resources/large/CEUTrio.HiSeq.WGS.b37.NA12878.20.21.bam
            final byte[] seq = "GTTTTGTTTACTACAGCTTTGTAGTAAATTTTGAACTCTAAAGTGTTAGTTCTCTAACTTTGTTTGTTTTTCAAGAGTGTTTTGACTCTTCTTACTGCATC".getBytes();
            final List<List<BwaMemAlignment>> allAlignments = aligner.alignSeqs(Collections.singletonList(seq));
            Assert.assertEquals(allAlignments.size(), 1);
            final List<BwaMemAlignment> alignments = allAlignments.get(0);
            Assert.assertEquals(alignments.size(), 1);
            final BwaMemAlignment alignment = alignments.get(0);
            Assert.assertEquals(index.getReferenceContigNames().get(alignment.getRefId()), "20");
            Assert.assertEquals(alignment.getCigar(), "101M");
            Assert.assertEquals(alignment.getMapQual(), 60);
            Assert.assertEquals(alignment.getRefStart(), 9999996);
            Assert.assertEquals(alignment.getNMismatches(), 0);
        }
    }

    public static void assertCorrectChimericContigAlignment(final BwaMemIndex index) {
        try ( final BwaMemAligner aligner = new BwaMemAligner(index) ) {
            // synthetic contig generated by extracting 300 bases from two different positions on chr20 in the reference
            final byte[] seq = "GTGGGAGAGAACTGGAACAAGAACCCAGTGCTCTTTCTGCTCTACCCACTGACCCATCCTCTCACGCATCATACACCCATACTCCCATCCACCCACCTTCCCATTCATGCATTCACCCATTCACCCACCTTCCATCCATCTACCATCCACCACGTACCTACACTCCCATCTACCATCCAACCACATTTCCATTCACCCATCCTCCCATCCATCAACCCTCCAATCCACCACCCACAGACCTTCCCATCCATTCATTTACCCATCCACATATTCACCCACCCTCCCATCCATCCATCTACTTGAAGCAGAATGAGGGAGGAAACCCAAGCCAGCTCGGGCTTGATCATGGCAGGCTTTGTCAGTCACTGTGAGTAACCAGACTTTATTTCAAGTGAGTTGGCCAACAGTGTCTCCCCAGTAGTAGAGTGATTGCTCGTCTGCCAGAAAAGGGGCACAGAGCTCTGGACATCAATACTTGCCGATCTCTCCTTCATCAGCCACCCAACCCTGGCAACAGTTTTCAATTACACCTGTGAAAACTTGTGGGTTAAACAGCTGAGATCCATGCCTCAGCTTCTATAGGTGATAAGCCTGTCCTTC".getBytes();
            final List<List<BwaMemAlignment>> allAlignments = aligner.alignSeqs(Collections.singletonList(seq));
            Assert.assertEquals(allAlignments.size(), 1);
            final List<BwaMemAlignment> alignments = allAlignments.get(0);
            Assert.assertEquals(alignments.size(), 2);
            final BwaMemAlignment alignment1;
            final BwaMemAlignment alignment2;
            if ( alignments.get(0).getRefStart() < alignments.get(1).getRefStart() ) {
                alignment1 = alignments.get(0);
                alignment2 = alignments.get(1);
            }
            else {
                alignment1 = alignments.get(1);
                alignment2 = alignments.get(0);
            }

            Assert.assertEquals(index.getReferenceContigNames().get(alignment1.getRefId()), "20");
            Assert.assertEquals(alignment1.getCigar(), "300M300S");
            Assert.assertEquals(alignment1.getMapQual(), 60);
            Assert.assertEquals(alignment1.getRefStart(), 999999);

            Assert.assertEquals(index.getReferenceContigNames().get(alignment2.getRefId()), "20");
            Assert.assertEquals(alignment2.getCigar(), "300S300M");
            Assert.assertEquals(alignment2.getMapQual(), 60);
            Assert.assertEquals(alignment2.getRefStart(), 1000999);
        }
    }
}
